// pages/write-replay/write-replay.js
import { commentCreate, ownList, commentDelete } from '../../api/apis.js'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    textInput: '',
    ownList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      did: options.did,
      title: options.title
    })
    this._getownList()
  },
  _input(e) {
    this.data.textInput = e.detail.value
  },
  setComment() {
    if (!this.data.textInput.trim()) {
      wx.showToast({
        title: '评论内容不能为空',
        icon: 'none'
      })
      return false
    }
    commentCreate({
      data: {
        content: this.data.textInput,
        id: this.data.did,
        openid: app.globalData.openid
      },
      success: res => {
        if(res.state) {
          this.setData({
            textInput: ''
          })
          this._getownList()
        }
      }
    })
  },
  _getownList() {
    ownList({
      data: {
        index: 1,
        size: 20,
        openid: app.globalData.openid,
        id: this.data.did
      },
      success: res => {
        if (res.state) {
          this.setData({
            ownList: res.data.list
          })
        }
      }
    })
  },
  _delete(e) {
    wx.showModal({
      title: '',
      content: '确定删除吗？',
      success: res => {
        console.log(res)
        if(res.confirm) {
          commentDelete({
            data: {
              openid: app.globalData.openid,
              id: e.currentTarget.dataset.id
            },
            success: res => {
              if (res.state) {
                wx.showToast({
                  title: '删除成功',
                  icon: 'none'
                })
                this._getownList()
              }
            }
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})